//卡务管理
angular.module('SmartCanteenApp.productionControl', ['ngRoute', 'ngTouch'])

    .config(['$routeProvider', function ($routeProvider) {
        $routeProvider.when('/productionPlan_manager', {
            templateUrl: 'productionControl/productionPlan_manager.html',
            controller: 'productionPlan_managerCtrl'
        });
         $routeProvider.when('/productionPlan_employee', {
            templateUrl: 'productionControl/productionPlan_employee.html',
            controller: 'productionPlan_employeeCtrl'
        });
        $routeProvider.when('/productionAdd', {
            templateUrl: 'productionControl/productionAdd.html',
            controller: 'productionAddCtrl'
        });
         $routeProvider.when('/productionUpdate/:planId', {
            templateUrl: 'productionControl/productionUpdate.html',
            controller: 'productionUpdateCtrl'
        });
        $routeProvider.when('/productionApproval/:planId', {
            templateUrl: 'productionControl/productionApproval.html',
            controller: 'productionApprovalCtrl'
        });
         $routeProvider.when('/productionLook/:planId', {
            templateUrl: 'productionControl/productionLook.html',
            controller: 'productionLookCtrl'
        });
    }])
	//生产计划_经理
    .controller('productionPlan_managerCtrl', ['$scope', '$timeout','$routeParams', function ($scope, $timeout,$routeParams) {
    	$(".modal-dialog").draggable();//为模态对话框添加拖拽
        $(".modal").css("overflow", "hidden");//禁止模态对话框的半透明背景滚动
        	
        	//格式化日期
	        $scope.formatDate =function(date){
	        	 var myyear = date.getFullYear();   
			    var mymonth = date.getMonth()+1;   
			    var myweekday = date.getDate();  
			    //alert("formatDate"+myyear+":"+mymonth+":"+myweekday)  
			    if(mymonth < 10){   
			        mymonth = "0" + mymonth;   
			    }   
			    if(myweekday < 10){   
			        myweekday = "0" + myweekday;   
			    }   
			    return (myyear+"-"+mymonth + "-" + myweekday);   
	        }
        	//获取与传入日期差几天的日期
	        $scope.getDisDate = function(range){
//	        	var timeDate = new Date(time); //传入日期   
				var timeDate = new Date();
	        	var wDate = new Date(timeDate.getFullYear(), timeDate.getMonth(), timeDate.getDate() + range );  
	    		return  $scope.formatDate(wDate); 
	        }
	        $('#datepicker').val( $scope.getDisDate( -7 ) );
        	$('#datepicker2').val( $scope.getNowFormatDate() );
        //日历选择
			$('#datepicker').datepicker({
			  autoclose: true
			});
			$('#datepicker2').datepicker({
			  autoclose: true
			});
			$scope.tabPage ='1';
			$scope.state = '1';//进页面的时候默认查询状态为1的  也就是已审批的
			$scope.goTab = function(page){
				$scope.tabPage = page
				if( page == '1'){
					$scope.state = '1';//已审批
					$scope.serachDatas();
					//$scope.pagination($scope.items1);
				}else if(page == '2'){
					$scope.state = '0';//待审批
					$scope.serachDatas();
					//$scope.pagination($scope.items2);
				}
//				else if(page == '3'){
//					$scope.state = '2';//作废
//					$scope.serachDatas();
//					//$scope.pagination($scope.items3);
//				}
			}
			//查询数据
			$scope.serachDatas = function(){
				$scope.getJson($scope.resultPath+"/wisdomeatery/v1/base_system/canteens/"+$scope.canteenId+"/production_plans?start_time="+$('#datepicker').val()+"&end_time="+$('#datepicker2').val()+"&status="+$scope.state,'',"GET",
        			function(data){
						console.log(data);
			 			if( data.code == 0){
			 				$scope.productionDatas = data.data;
			 				$scope.pagination($scope.productionDatas);
			 			}else{
			 				console.log(data.msg);
			 			}
			 		});
			}
			if( $scope.getCanteenId() != ''){
   				$scope.canteenId =  $scope.getCanteenId() ;
   				$scope.serachDatas();
   			}
		   	$scope.$on('to-child', function(event,data) {
						 //获取从父控制器传来的当前选中食堂
						$scope.canteenId = data;
						$scope.serachDatas();
				});
			
			
			$scope.plan_id = '';
			$scope.plan_date = '';
			$scope.plan_name = '';
			//删除生产计划
			$scope.delProduction = function(plan_id,date,name){
				$scope.plan_id = plan_id ;
				$scope.plan_date = date ;
				$scope.plan_name = name ;
				$('#delPlan').modal('toggle');
				
			}
			//删除提示框确定键
			$scope.del_plan = function(){
				$scope.getJson($scope.resultPath+"/wisdomeatery/v1/base_system/canteens/"+$scope.canteenId+"/production_plans/"+$scope.plan_id,'',"DELETE",
        			function(data){
						console.log(data);
			 			if( data.code == 0){
			 				$scope.showSuccess('操作成功！');
			 				$scope.serachDatas();
			 				$('#delPlan').modal('toggle');
			 			}else{
			 				console.log(data.msg);
			 			}
			 		});
			}
			
			//查询按钮
			$scope.search = function(){
				$scope.serachDatas();
			}
			$scope.initial = '10';
    }])
    //生产计划_申请人
    .controller('productionPlan_employeeCtrl', ['$scope', '$timeout','$routeParams', function ($scope, $timeout,$routeParams) {
    	$(".modal-dialog").draggable();//为模态对话框添加拖拽
        $(".modal").css("overflow", "hidden");//禁止模态对话框的半透明背景滚动
        	
        	//格式化日期
	        $scope.formatDate =function(date){
	        	 var myyear = date.getFullYear();   
			    var mymonth = date.getMonth()+1;   
			    var myweekday = date.getDate();  
			    //alert("formatDate"+myyear+":"+mymonth+":"+myweekday)  
			    if(mymonth < 10){   
			        mymonth = "0" + mymonth;   
			    }   
			    if(myweekday < 10){   
			        myweekday = "0" + myweekday;   
			    }   
			    return (myyear+"-"+mymonth + "-" + myweekday);   
	        }
        	//获取与传入日期差几天的日期
	        $scope.getDisDate = function(range){
//	        	var timeDate = new Date(time); //传入日期   
				var timeDate = new Date();
	        	var wDate = new Date(timeDate.getFullYear(), timeDate.getMonth(), timeDate.getDate() + range );  
	    		return  $scope.formatDate(wDate); 
	        }
	        $('#datepicker').val( $scope.getDisDate( -7 ) );
        	$('#datepicker2').val( $scope.getNowFormatDate() );
        //日历选择
			$('#datepicker').datepicker({
			  autoclose: true
			});
			$('#datepicker2').datepicker({
			  autoclose: true
			});
			$scope.tabPage ='1';
			$scope.state = '1';//进页面的时候默认查询状态为1的  也就是已审批的
			$scope.goTab = function(page){
				$scope.tabPage = page
				if( page == '1'){
					$scope.state = '1';//已审批
					$scope.serachDatas();
					//$scope.pagination($scope.items1);
				}else if(page == '2'){
					$scope.state = '0';//待审批
					$scope.serachDatas();
					//$scope.pagination($scope.items2);
				}
//				else if(page == '3'){
//					$scope.state = '2';//作废
//					$scope.serachDatas();
//					//$scope.pagination($scope.items3);
//				}
			}
			//查询数据
			$scope.serachDatas = function(){
				console.log($scope.state);
				$scope.getJson($scope.resultPath+"/wisdomeatery/v1/base_system/canteens/"+$scope.canteenId+"/production_plans?start_time="+$('#datepicker').val()+"&end_time="+$('#datepicker2').val()+"&status="+$scope.state,'',"GET",
        			function(data){
						console.log(data);
			 			if( data.code == 0){
			 				$scope.productionDatas = data.data;
			 				$scope.pagination($scope.productionDatas);
			 			}else{
			 				console.log(data.msg);
			 			}
			 		});
			}
			if( $scope.getCanteenId() != ''){
   				$scope.canteenId =  $scope.getCanteenId() ;
   				$scope.serachDatas();
   			}
		   	$scope.$on('to-child', function(event,data) {
						 //获取从父控制器传来的当前选中食堂
						$scope.canteenId = data;
						$scope.serachDatas();
				});
			
			
			$scope.plan_id = '';
			$scope.plan_date = '';
			$scope.plan_name = '';
			//删除生产计划
			$scope.delProduction = function(plan_id,date,name){
				$scope.plan_id = plan_id ;
				$scope.plan_date = date ;
				$scope.plan_name = name ;
				$('#delPlan').modal('toggle');
				
			}
			//删除提示框确定键
			$scope.del_plan = function(){
				$scope.getJson($scope.resultPath+"/wisdomeatery/v1/base_system/canteens/"+$scope.canteenId+"/production_plans/"+plan_id,'',"DELETE",
        			function(data){
						console.log(data);
			 			if( data.code == 0){
			 				$scope.showSuccess('操作成功！');
			 				$scope.serachDatas();
			 			}else{
			 				console.log(data.msg);
			 			}
			 		});
			}
			
			//查询按钮
			$scope.search = function(){
				$scope.serachDatas();
			}
			$scope.initial = '10';
			
			
    	
    }])
    
   //生产新增
  .controller('productionAddCtrl', ['$scope', '$timeout','$routeParams', function ($scope, $timeout,$routeParams) {
  		$(".modal-dialog").draggable();//为模态对话框添加拖拽
        $(".modal").css("overflow", "hidden");//禁止模态对话框的半透明背景滚动
  	 //日历选择
			$('#datepicker').datepicker({
			  autoclose: true
			});
			
			//获取两个时间差几天
        $scope.distance_days = function(end_str,sta_str){
        	//结束时间  
			var end_date = new Date(end_str);//将字符串转化为时间  
			//开始时间  
			var sta_date = new Date(sta_str);  
			var num = (end_date-sta_date)/(1000*3600*24);//求出两个时间的时间差，这个是天数  
			var days = parseInt(Math.ceil(num));//转化为整天（小于零的话剧不用转了）  
			return days;
        }
        
    	//查询某一食堂下所有餐别信息
        $scope.getPeriod_time = function(){
        	$scope.getJson($scope.resultPath+"/wisdomeatery/v1/base_system/canteens/"+$scope.canteenId+"/periodTimes",'',"GET",
        			function(data){
						console.log(data);
			 			if( data.code == 0){
			 					$scope.periods = data.data;
			 					if( $scope.periods.length != 0){
			 						$scope.periodId = $scope.periods[0].period_time_id ;
			 					}
			 			}else{
			 				console.log(data.msg);
			 			}
			 		});
        }
        //获取食堂下所有菜品类别分类
   			$scope.getCategorys = function(){
   				$scope.getJson($scope.resultPath+"/wisdomeatery/v1/menus/canteens/"+$scope.canteenId+"/categorys",'',"GET",function(data){
							console.log(data);
				 			if( data.code == 0){
				 					$scope.categorys = data.data;  
				 			}else{
				 				console.log(data.msg);
				 			}
				 		});
   			}
   			//获取某食堂下所有菜品
   			$scope.getDishes = function(){
   				$scope.getJson($scope.resultPath+"/wisdomeatery/v1/menus/canteens/"+$scope.canteenId+"/items",'',"GET",function(data){
							console.log(data);
				 			if( data.code == 0){
				 					$scope.classDishes = data.data;  
				 					//$scope.classintial = data.data ;
    								//$scope.classcurrentData = data.data ;
				 			}else{
				 				console.log(data.msg);
				 			}
				 		});
   			}
        if( $scope.getCanteenId() != ''){
   				$scope.canteenId =  $scope.getCanteenId() ;
   				//先获取餐别信息
        		$scope.getPeriod_time();
		 		 $scope.getCategorys();//查询菜品分类
		 		 $scope.getDishes();//查询所有菜品
   			}
		   	$scope.$on('to-child', function(event,data) {
						 //获取从父控制器传来的当前选中食堂
						$scope.canteenId = data;
						//先获取餐别信息
        				$scope.getPeriod_time();
				 		$scope.getCategorys();//查询菜品分类
		 				$scope.getDishes();//查询所有菜品
				});
				$scope.datas = [];
				$scope.datas.items = [];
				$scope.lock = false;//默认为未锁定状态
				$scope.submitData = {
									 period_time_id:"",
									 pointed_date:"",
									 items:[]
									}
		//查询指定菜谱计划信息,同时锁定日期餐别
		$scope.getPeriodDatas = function(event){
			event.stopPropagation();
			if( isNull( $('#datepicker').val() ) == true){
       			$scope.showError('生产计划日期不能为空');
       			return;
       		}
			var distance = $scope.distance_days( $('#datepicker').val() , $scope.getNowFormatDate()) ;
			if( distance < 0){
				$scope.showError('生产计划日期必须大于等于今天');
       			return;
			}
			var postData = {
				selectDate : $('#datepicker').val() ,
				periodTimeId : $scope.periodId
			};
			console.log(postData);
			$scope.lock = true;
			$scope.getJson($scope.resultPath+"/wisdomeatery/v1/menus/canteens/"+$scope.canteenId+"/menuPlansDate",postData,"POST",function(data){
							console.log(data);
				 			if( data.code == 0){
				 					$scope.datas = data.data[0] ;
				 					if( data.data.length == 0 ){
				 						$scope.submitData.period_time_id = $scope.periodId ;
				 						$scope.submitData.pointed_date = $('#datepicker').val();
				 						$scope.datas = $scope.submitData ;
				 						$('#contentHtml').html('未找到菜谱计划中菜品信息');
										$('#warningChoose').modal('toggle');
										return;
				 					}
				 			}else{
				 				console.log(data.msg);
				 			}
				 		});
		}
		$scope.unlock = function(){
			$scope.lock = false;
		}
		//生产新增添加菜品
		$scope.addPro = function(){
			console.log($scope.datas);
			console.log($scope.datas.period_time_id);
			if( $scope.datas.period_time_id == '' || $scope.datas.period_time_id == undefined){
				$scope.showError('请先锁定日期及餐别后，再进行菜品的添加');
				return;
			}
			$('#choiceDishes').modal('toggle');
			$scope.maDatas = [];
		}
		//生产新增删除菜品
		$scope.addDel = function(id){
			console.log(id);
			console.log($scope.datas.items);
			var arr = $scope.datas.items ;
			for( var i = 0; i< arr.length ; i++){
				if( arr[i].id == id){
					arr.splice(i,1);
				}
			}
			console.log(arr);
		}
		
		$scope.intialOneCategory = [];
		 //根据点选的分类名称，获取该分类下所有菜品
		$scope.getDishesByCategory = function(id){
			var arr = $scope.classDishes ;
			var newList = [];
			for(var i = 0 ;i<arr.length ; i++){
				if(arr[i].category_id == id){
					newList.push(arr[i]);
				}
			}
			console.log(newList);
			$scope.dishesByOneCategory = newList ;
			$scope.intialOneCategory = newList ;
		}
       
        //物料查询初始值默认为 ‘’
        $scope.goods = '';
       //点击菜品添加
	        $scope.cmaterial = function(event,id,name,unit,window_name){
	        	event.stopPropagation();
	        	var standards = document.getElementsByClassName('maDataList_name');
	        	var intial = '0';//默认标准成分模板中没有
	        	for(var i = 0; i< standards.length ; i++){
	        		if(standards[i].innerHTML == name){
	        			intial = '1'; //如果模板中已经有这个成分了 就置为1
	        		}
	        	}
	        	if( intial == '0'){//说明模板中没有，需要添加material_id
	        			$scope.maDatas.push( { "id": id,"name": name, "unit":unit, "count":1, "windowName":window_name});
				}
	        	console.log($scope.maDatas);
	        }
	        
	 		$scope.maDatas = [];
	 		//菜肴管理中删除此行
	        $scope.delThis = function(event,name){
	        	event.stopPropagation();
	        	var target = event.target;
	        	var arr = $scope.maDatas ;
	        	console.log(arr);
	        	console.log(name);
				 for(var i=0; i<arr.length; i++) {
				    if(arr[i].name == name) {
				      arr.splice(i, 1);
				      break;
				    }
				  }
				 $scope.maDatas = arr ;
	        }
       		//$scope.addDishesArray = [];
       		//菜品选择模板确定键
       		$scope.addDish_true = function(){
       			
       			
       			var arr = $scope.maDatas ;
       			var arrItems  =  $scope.datas.items;
       			
       			for(var i = 0; i<arr.length ; i++){
       				if( arr[i].count <= 0 ){
   						$scope.showError('菜品数量必须大于0');
       					return;
   					}
       				if( !isNumber( arr[i].count+'' ) ){
       					$scope.showError('菜品数量必须为正整数');
       					return;
       				}
       			}
       			
       			for(var i = 0; i<arrItems.length ; i++){
       				for(var j = 0; j<arr.length ; j++ ){
       					if( arr[j].id == arrItems[i].id ){
       						$scope.showError('计划中已存在'+arrItems[i].name+',不能添加');
           					return;
       					}
       				}
       			}
       			console.log($scope.maDatas);
       			
         		$scope.datas.items = $scope.datas.items.concat($scope.maDatas);
       			console.log($scope.datas.items);
       			$('#choiceDishes').modal('toggle');
       		}
       		//提交
       		$scope.dishInforSave = function(){
       			console.log($scope.datas);
       			console.log($scope.datas.items.length);
       			if( $scope.datas.items.length == 0 ){
       				$scope.showError('菜品信息不能为空');
   					return;
       			}
       			if( $scope.lock == false ){
       				$scope.showError('请先锁定日期及餐别');
   					return;
       			}
       			$scope.datas.remark = $scope.remark ;
       			console.log($scope.datas);
       			console.log($scope.remark);
       			console.log($scope.canteenId);
       			$scope.getJson($scope.resultPath+"/wisdomeatery/v1/base_system/canteens/"+$scope.canteenId+"/production_plans",$scope.datas,"POST",function(data){
							console.log(data);
				 			if( data.code == 0){
				 				$scope.showSuccess('操作成功！');
				 				$scope.jump_back();
				 			}else{
				 				$scope.showError(data.msg);
				 			}
				 		});
       		}
		//当循环执行结束时执行
        $scope.$on('ngRepeatFinishedone', function (ngRepeatFinishedEvent) {
               $(".timepicker").timepicker({
					showInputs: false,
					showMeridian:false	
				});
        });
    	
    }])
  
  	//生产维护
  .controller('productionUpdateCtrl', ['$scope', '$timeout','$routeParams', function ($scope, $timeout,$routeParams) {
  		$(".modal-dialog").draggable();//为模态对话框添加拖拽
        $(".modal").css("overflow", "hidden");//禁止模态对话框的半透明背景滚动
         $scope.planId = $routeParams.planId ;//计划单号
        console.log($scope.planId);
			//获取两个时间差几天
        $scope.distance_days = function(end_str,sta_str){
        	//结束时间  
			var end_date = new Date(end_str);//将字符串转化为时间  
			//开始时间  
			var sta_date = new Date(sta_str);  
			var num = (end_date-sta_date)/(1000*3600*24);//求出两个时间的时间差，这个是天数  
			var days = parseInt(Math.ceil(num));//转化为整天（小于零的话剧不用转了）  
			return days;
        }
    	//查询某一食堂下所有餐别信息
        $scope.getPeriod_time = function(){
        	$scope.getJson($scope.resultPath+"/wisdomeatery/v1/base_system/canteens/"+$scope.canteenId+"/periodTimes",'',"GET",
        			function(data){
						console.log(data);
			 			if( data.code == 0){
			 					$scope.periods = data.data;
			 					if( $scope.periods.length != 0){
			 						$scope.periodId = $scope.periods[0].period_time_id ;
			 					}
			 			}else{
			 				console.log(data.msg);
			 			}
			 		});
        }
        //获取食堂下所有分类
   			$scope.getCategorys = function(){
   				$scope.getJson($scope.resultPath+"/wisdomeatery/v1/menus/canteens/"+$scope.canteenId+"/categorys",'',"GET",function(data){
							console.log(data);
				 			if( data.code == 0){
				 					$scope.categorys = data.data;  
				 			}else{
				 				console.log(data.msg);
				 			}
				 		});
   			}
   			//获取某食堂下所有菜品
   			$scope.getDishes = function(){
   				$scope.getJson($scope.resultPath+"/wisdomeatery/v1/menus/canteens/"+$scope.canteenId+"/items",'',"GET",function(data){
							console.log(data);
				 			if( data.code == 0){
				 					$scope.classDishes = data.data;  
				 					//$scope.classintial = data.data ;
    								//$scope.classcurrentData = data.data ;
				 			}else{
				 				console.log(data.msg);
				 			}
				 		});
   			}
        if( $scope.getCanteenId() != ''){
   				$scope.canteenId =  $scope.getCanteenId() ;
   				//先获取餐别信息
        		$scope.getPeriod_time();
		 		 $scope.getCategorys();//查询菜品分类
		 		 $scope.getDishes();//查询所有菜品
   			}
		   	$scope.$on('to-child', function(event,data) {
						 //获取从父控制器传来的当前选中食堂
						$scope.canteenId = data;
						//先获取餐别信息
        				$scope.getPeriod_time();
				 		$scope.getCategorys();//查询菜品分类
		 				$scope.getDishes();//查询所有菜品
				});
				$scope.datas = [];
				$scope.periodName = '';
				$scope.pointDate = '';
				$scope.period_time_id = '';
				
		//查询某个生产计划的详细信息
        $scope.getJson($scope.resultPath+"/wisdomeatery/v1/base_system/canteens/"+$scope.canteenId+"/production_plans/"+$scope.planId,'',"GET",
        			function(data){
						console.log(data);
			 			if( data.code == 0){
			 				$scope.datas = data.data;
			 				$scope.periodName = data.data[0].period_time_name;
			 				$scope.pointDate = data.data[0].selected_date;
			 				$scope.period_time_id = data.data[0].period_time_id ;
			 				$scope.remark = data.data[0].remark ;
			 				$scope.manufacture_time = data.data[0].manufacture_time ;
			 			}else{
			 				console.log(data.msg);
			 			}
			 		});	
			 		
		//修改生产计划信息
		$scope.productionUpdate = function(){
			var postData = {
				'period_time_id':$scope.period_time_id,
				'selected_date':$scope.pointDate,
				'items':$scope.datas,
				'remark':$scope.remark
			}
			
			$scope.getJson($scope.resultPath+"/wisdomeatery/v1/base_system/canteens/"+$scope.canteenId+"/production_plans/"+$scope.planId,postData,"PUT",
        			function(data){
						console.log(data);
			 			if( data.code == 0){
			 				$scope.showSuccess('操作成功！');
			 				$scope.jump_back();
			 			}else{
			 				console.log(data.msg);
			 			}
			 		});	
		}
		
		
		
		//查询指定菜谱计划信息
		$scope.getPeriodDatas = function(){
			if( isNull( $('#datepicker').val() ) == true){
       			$scope.showError('生产计划日期不能为空');
       			return;
       		}
			
			var distance = $scope.distance_days( $('#datepicker').val() , $scope.getNowFormatDate()) ;
			if( distance < 0){
				$scope.showError('生产计划日期必须大于等于今天');
       			return;
			}
			var postData = {
				selectDate : $('#datepicker').val() ,
				periodTimeId : $scope.periodId
			};
			console.log(postData);
			$scope.getJson($scope.resultPath+"/wisdomeatery/v1/menus/canteens/"+$scope.canteenId+"/menuPlansDate",postData,"POST",function(data){
							console.log(data);
				 			if( data.code == 0){
				 					$scope.datas = data.data[0] ;
				 			}else{
				 				console.log(data.msg);
				 			}
				 		});
//			console.log($scope.periodId);
//			console.log( $('#datepicker').val() );
		}
		//生产新增添加菜品
		$scope.addPro = function(){
//			if( $scope.datas.id == '' || $scope.datas.id == undefined){
//				$scope.showError('请先确定日期及餐别后，再进行菜品的添加');
//				return;
//			}
			$('#choiceDishes').modal('toggle');
			$scope.maDatas = [];
		}
		//生产新增删除菜品
		$scope.addDel = function(id){
			console.log(id);
			console.log($scope.datas);
			var arr = $scope.datas ;
			for( var i = 0; i< arr.length ; i++){
				if( arr[i].item_id == id){
					arr.splice(i,1);
				}
			}
			console.log(arr);
		}
		
		$scope.intialOneCategory = [];
		 //根据点选的分类名称，获取该分类下所有菜品
		$scope.getDishesByCategory = function(id){
			var arr = $scope.classDishes ;
			var newList = [];
			for(var i = 0 ;i<arr.length ; i++){
				if(arr[i].category_id == id){
					newList.push(arr[i]);
				}
			}
			console.log(newList);
			$scope.dishesByOneCategory = newList ;
			$scope.intialOneCategory = newList ;
		}
       
        //物料查询初始值默认为 ‘’
        $scope.goods = '';
       //点击菜品添加
	        $scope.cmaterial = function(event,id,name,unit,window_name){
	        	event.stopPropagation();
	        	var standards = document.getElementsByClassName('maDataList_name');
	        	var intial = '0';//默认标准成分模板中没有
	        	for(var i = 0; i< standards.length ; i++){
	        		if(standards[i].innerHTML == name){
	        			intial = '1'; //如果模板中已经有这个成分了 就置为1
	        		}
	        	}
	        	if( intial == '0'){//说明模板中没有，需要添加material_id
	        			$scope.maDatas.push( { "item_id": id,"item_name": name, "item_unit":unit, "plan_count":1, "window_name":window_name,"manufacture_time":$scope.manufacture_time});
				}
	        }
	        
	 		$scope.maDatas = [];
	 		//菜肴管理中删除此行
	        $scope.delThis = function(event,name){
	        	event.stopPropagation();
	        	var target = event.target;
	        	var arr = $scope.maDatas ;
	        	console.log(arr);
	        	console.log(name);
				 for(var i=0; i<arr.length; i++) {
				    if(arr[i].name == name) {
				      arr.splice(i, 1);
				      break;
				    }
				  }
				 $scope.maDatas = arr ;
	        }
       		//$scope.addDishesArray = [];
       		//菜品选择模板确定键
       		$scope.addDish_true = function(){
       			var arr = $scope.maDatas ;
       			var arrItems  =  $scope.datas ;
       			for(var i = 0; i<arr.length ; i++){
       				if( arr[i].plan_count <= 0 ){
   						$scope.showError('菜品数量必须大于0');
       					return;
   					}
       				if( !isNumber( arr[i].plan_count+'' ) ){
       					$scope.showError('菜品数量必须为正整数');
       					return;
       				}
       			}
       			for(var i = 0; i<arrItems.length ; i++){
       				for(var j = 0; j<arr.length ; j++ ){
       					if( arr[j].item_id == arrItems[i].item_id ){
       						$scope.showError('计划中已存在'+arrItems[i].item_name+',不能添加');
           					return;
       					}
       				}
       			}
         		$scope.datas = $scope.datas.concat($scope.maDatas);
       			console.log($scope.datas );
       			$('#choiceDishes').modal('toggle');
       		}
       
		//当循环执行结束时执行
        $scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) {
               $(".timepicker").timepicker({
					showInputs: false,
					showMeridian:false	
				});
        });
    	
    }])
  
  //生产审批
  .controller('productionApprovalCtrl', ['$scope', '$timeout','$routeParams', function ($scope, $timeout,$routeParams) {
  		$(".modal-dialog").draggable();//为模态对话框添加拖拽
        $(".modal").css("overflow", "hidden");//禁止模态对话框的半透明背景滚动
        
        $scope.planId = $routeParams.planId ;//计划单号
        console.log($scope.planId);
		$scope.periodName = '';
		$scope.pointDate = '';
		$scope.datas = [];
		$scope.period_time_id = '';
		$scope.manufacture_time = ''; 
        //获取食堂下所有分类
   			$scope.getCategorys = function(){
   				$scope.getJson($scope.resultPath+"/wisdomeatery/v1/menus/canteens/"+$scope.canteenId+"/categorys",'',"GET",function(data){
							console.log(data);
				 			if( data.code == 0){
				 					$scope.categorys = data.data;  
				 			}else{
				 				console.log(data.msg);
				 			}
				 		});
   			}
   			//获取某食堂下所有菜品
   			$scope.getDishes = function(){
   				$scope.getJson($scope.resultPath+"/wisdomeatery/v1/menus/canteens/"+$scope.canteenId+"/items",'',"GET",function(data){
							console.log(data);
				 			if( data.code == 0){
				 					$scope.classDishes = data.data;  
				 					//$scope.classintial = data.data ;
    								//$scope.classcurrentData = data.data ;
				 			}else{
				 				console.log(data.msg);
				 			}
				 		});
   			}
        if( $scope.getCanteenId() != ''){
   				$scope.canteenId =  $scope.getCanteenId() ;
		 		 $scope.getCategorys();//查询菜品分类
		 		 $scope.getDishes();//查询所有菜品
   			}
		   	$scope.$on('to-child', function(event,data) {
						 //获取从父控制器传来的当前选中食堂
						$scope.canteenId = data;
				 		$scope.getCategorys();//查询菜品分类
		 				$scope.getDishes();//查询所有菜品
				});
				
		if( $scope.getCanteenId() != ''){
   				$scope.canteenId =  $scope.getCanteenId() ;
   			}
		   	$scope.$on('to-child', function(event,data) {
						 //获取从父控制器传来的当前选中食堂
						$scope.canteenId = data;
				});
		
		//查询某个生产计划的详细信息
        $scope.getJson($scope.resultPath+"/wisdomeatery/v1/base_system/canteens/"+$scope.canteenId+"/production_plans/"+$scope.planId,'',"GET",
        			function(data){
						console.log(data);
			 			if( data.code == 0){
			 				$scope.datas = data.data;
			 				$scope.period_time_id = data.data[0].period_time_id;
			 				$scope.periodName = data.data[0].period_time_name;
			 				$scope.pointDate = data.data[0].selected_date;
			 				$scope.manufacture_time = data.data[0].manufacture_time ;
			 			}else{
			 				console.log(data.msg);
			 			}
			 		});
			 		
		//当循环执行结束时执行
        $scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) {
               $(".timepicker").timepicker({
					showInputs: false,
					showMeridian:false	
				});
        });	 		
			 		
		//生产新增添加菜品
		$scope.addPro = function(){
//			if( $scope.datas.id == '' || $scope.datas.id == undefined){
//				$scope.showError('请先确定日期及餐别后，再进行菜品的添加');
//				return;
//			}
			$('#choiceDishes').modal('toggle');
			$scope.maDatas = [];
		}
		//生产新增删除菜品
		$scope.addDel = function(id){
			console.log(id);
			console.log($scope.datas);
			var arr = $scope.datas ;
			for( var i = 0; i< arr.length ; i++){
				if( arr[i].item_id == id){
					arr.splice(i,1);
				}
			}
			console.log(arr);
		}
		
		$scope.intialOneCategory = [];
		 //根据点选的分类名称，获取该分类下所有菜品
		$scope.getDishesByCategory = function(id){
			var arr = $scope.classDishes ;
			console.log(arr);
			var newList = [];
			for(var i = 0 ;i<arr.length ; i++){
				if(arr[i].category_id == id){
					newList.push(arr[i]);
				}
			}
			console.log(newList);
			$scope.dishesByOneCategory = newList ;
			$scope.intialOneCategory = newList ;
		}
       
        //物料查询初始值默认为 ‘’
        $scope.goods = '';
       //点击菜品添加
	        $scope.cmaterial = function(event,id,name,unit,window_name){
	        	event.stopPropagation();
	        	var standards = document.getElementsByClassName('maDataList_name');
	        	var intial = '0';//默认标准成分模板中没有
	        	for(var i = 0; i< standards.length ; i++){
	        		if(standards[i].innerHTML == name){
	        			intial = '1'; //如果模板中已经有这个成分了 就置为1
	        		}
	        	}
	        	if( intial == '0'){//说明模板中没有，需要添加material_id
	        			$scope.maDatas.push( { "item_id": id,"item_name": name, "item_unit":unit, "plan_count":1, "window_name":window_name,"manufacture_time":$scope.manufacture_time});
				}
	        }
	        
	 		$scope.maDatas = [];
	 		//菜肴管理中删除此行
	        $scope.delThis = function(event,name){
	        	event.stopPropagation();
	        	var target = event.target;
	        	var arr = $scope.maDatas ;
	        	console.log(arr);
	        	console.log(name);
				 for(var i=0; i<arr.length; i++) {
				    if(arr[i].name == name) {
				      arr.splice(i, 1);
				      break;
				    }
				  }
				 $scope.maDatas = arr ;
	        }
       		//$scope.addDishesArray = [];
       		//菜品选择模板确定键
       		$scope.addDish_true = function(){
       			var arr = $scope.maDatas ;
       			var arrItems  =  $scope.datas ;
       			console.log(arr);
       			console.log(arrItems);
       			for(var i = 0; i<arr.length ; i++){
       				if( arr[i].plan_count <= 0 ){
   						$scope.showError('菜品数量必须大于0');
       					return;
   					}
       				if( !isNumber( arr[i].plan_count+'' ) ){
       					$scope.showError('菜品数量必须为正整数');
       					return;
       				}
       			}
       			for(var i = 0; i<arrItems.length ; i++){
       				for(var j = 0; j<arr.length ; j++ ){
       					if( arr[j].item_id == arrItems[i].item_id ){
       						$scope.showError('计划中已存在'+arrItems[i].item_name+',不能添加');
           					return;
       					}
       				}
       			}
         		$scope.datas = $scope.datas .concat($scope.maDatas);
       			console.log($scope.datas );
       			$('#choiceDishes').modal('toggle');
       		} 		
			 		
			 		
		//审批
		$scope.approval = function(){
//			console.log($scope.datas);
//			var postData = {
//						items:$scope.datas
//			}
//			 $scope.getJson($scope.resultPath+"/wisdomeatery/v1/base_system/canteens/"+$scope.canteenId+"/production_plans_audit/"+$scope.planId,postData,"PUT",
//      			function(data){
//						console.log(data);
//			 			if( data.code == 0){
//			 				$scope.showSuccess('操作成功！');
//			 				$scope.jump_back();
//			 			}else{
//			 				$scope.showError(data.msg);
//			 			}
//			 		});
			 		
			var postData = {
				'period_time_id':$scope.period_time_id,
				'selected_date':$scope.pointDate,
				'items':$scope.datas,
				'remark':$scope.remark
			}
			console.log(postData);
			$scope.getJson($scope.resultPath+"/wisdomeatery/v1/base_system/canteens/"+$scope.canteenId+"/production_plans_audit/"+$scope.planId,postData,"PUT",
        			function(data){
						console.log(data);
			 			if( data.code == 0){
			 				$scope.showSuccess('操作成功！');
			 				$scope.jump_back();
			 			}else{
			 				console.log(data.msg);
			 			}
			 		});	
			 		
		}
		
		
		
		
		
		
		
		
		
		
		
    	
    }])
  
  //生产查看
  .controller('productionLookCtrl', ['$scope', '$timeout','$routeParams', function ($scope, $timeout,$routeParams) {
  		$(".modal-dialog").draggable();//为模态对话框添加拖拽
        $(".modal").css("overflow", "hidden");//禁止模态对话框的半透明背景滚动
        
         $scope.planId = $routeParams.planId ;//计划单号
        console.log($scope.planId);
        
        
		$scope.periodName = '';//暂时写死早餐，后期查询赋值	
		$scope.pointDate = '';
		$scope.datas = [];
		
		if( $scope.getCanteenId() != ''){
   				$scope.canteenId =  $scope.getCanteenId() ;
   			}
		   	$scope.$on('to-child', function(event,data) {
						 //获取从父控制器传来的当前选中食堂
						$scope.canteenId = data;
				});
		
		//查询某个生产计划的详细信息
        $scope.getJson($scope.resultPath+"/wisdomeatery/v1/base_system/canteens/"+$scope.canteenId+"/production_plans/"+$scope.planId,'',"GET",
        			function(data){
						console.log(data);
			 			if( data.code == 0){
			 				$scope.datas = data.data;
			 				$scope.periodName = data.data[0].period_time_name;
			 				$scope.pointDate = data.data[0].selected_date;
			 				$scope.remark = data.data[0].remark ;
			 			}else{
			 				console.log(data.msg);
			 			}
			 		});
		
		
    	
    }])
  		

